<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="170px"
      class="demo-ruleForm"
      size="mini"
    >

      <el-row>
        <el-col :span="17">
          <el-form-item label="审批意见:" prop="remark">
            <el-input type="textarea" placeholder="请输入审批意见" :autosize="{ minRows: 4, maxRows: 4}" v-model="ruleForm.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item>
        <el-button @click="resetForm('ruleForm')">{{$t("action.reset")}}</el-button>
        <el-button type="primary" @click="submitForm('ruleForm',1)">{{$t("action.save")}}</el-button>

        <el-button type="danger" @click="submitForm('ruleForm',2)">驳回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import Cookies from "js-cookie";
  import ethnic from "@/assets/json/ethnic.js";
  import { format } from "@/utils/datetime";

  export default {
    name: "edit",
    props: {
      msg: ""
    },
    data() {
      return {
        url: this.$Config.url,
        ethnicList: [], //名族列表
        schoolList: [], //机构列表
        gradeData: [], //年级列表
        classData: [], //班级列表
        id: "", //修改对应的id
        ifSchoolId: false,
        ruleForm: {
          remark:""//备注
        },
        rules: {
          remark: [
            { required: true, message: "请输入审批意见", trigger: "blur" },
          ],
        },
        formData: {}
      };
    },
    mounted() {
      this.ethnicList = ethnic;
      this.getSchool();
      this.schoolId();
      this.getClassId();
    },
    methods: {
      getClassId(){
        const that = this;
        this.$axios
          .get(that.url + "/admin/productclass/list", {
            headers: {
              "x-baie-admin-token": Cookies.get("token")
            }
          })
          .then(function(res) {
            if (res.data.code == 200) {
              that.classData = res.data.data;
            } else {
              that.$message({
                message: res.data.msg,
                type: "warning"
              });
            }
          })
          .catch(function(error) {
            console.log(error);
          });
      },

      schoolId() {
        try {
          const deptId = JSON.parse(localStorage.getItem("userInfo")).deptId;
          if (deptId == null || deptId == "") {
            this.ifSchoolId = false;
            this.ruleForm.deptId='';
          } else {
            this.ifSchoolId = true;//不能点击
          }

          this.ruleForm.deptId = deptId;
        } catch (error) {}
      },
      getSchool() {
        const that = this;
        this.$axios
          .get(that.url + "/admin/dept/list", {
            headers: {
              "x-baie-admin-token": Cookies.get("token")
            }
          })
          .then(function(res) {
            if (res.data.code == 200) {
              that.schoolList = res.data.data;
            } else {
              that.$message({
                message: res.data.msg,
                type: "warning"
              });
            }
          })
          .catch(function(error) {
            console.log(error);
          });
      },
      resetData(id = 1) {

        this.$refs["ruleForm"].resetFields();//先清空
        const that = this;
        if (id) {
          this.id = id;
        }

      },
      submitForm(formName,type) {
        const that = this;
        this.$refs[formName].validate(valid => {
          if (valid) {
            that.send(type);
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      send(type) {
        const that = this;

        const deptId = JSON.parse(localStorage.getItem("userInfo")).deptId;
        let lj = '/refundorder/audit'
        if(type == 2){
          lj = '/refundorder/auditFail';
        }
        const data={
          refundOrdreId:this.id,
          remark:that.ruleForm.remark
        }
        this.$axios
          .post(
            that.url + lj,
            that.$qs.stringify(data),
            {
              headers: {
                "x-baie-admin-token": Cookies.get("token")
              }
            }
          )
          .then(function(res) {
            if (res.status == 200) {
              if(res.data.code==200){
                if (that.msg == 1) {
                  that.$message({
                    message: "提交成功",
                    type: "success"
                  });
                } else {
                  that.$message({
                    message:"提交成功",
                    type: "success"
                  });
                }
                that.$emit("hideEdit", false);
              }else{
                that.$message({
                  message: res.data.msg,
                  type: "warning"
                });
              }
            } else {
              that.$message({
                message: res.statusText,
                type: "warning"
              });
            }
          })
          .catch(function(error) {
            console.log(error);
          });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.id="";
        this.schoolId();
      }
    }
  };
</script>

<style scoped lang="scss">
  .el-select,
  .el-date-editor {
    width: 100%;
  }
</style>
